package org.pettyfox.pfpay.biz;

import org.pettyfox.pfpay.controller.admin.AuditRest;
import org.pettyfox.pfpay.entity.PayOrder;
import org.pettyfox.pfpay.mapper.PayOrderMapper;
import org.pettyfox.pfpay.notify.DefaultPFNotify;
import org.pettyfox.pfpay.pojo.PayAuditPojo;
import org.pettyfox.pfpay.pojo.api.Pay;
import org.pettyfox.pfpay.pojo.api.PayAlonePojo;
import org.pettyfox.pfpay.utils.DateFormat;
import org.pettyfox.pfpay.utils.MD5Utils;
import org.pettyfox.pfpay.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
public class PayOrderBiz extends BaseBiz<PayOrderMapper,PayOrder> {
    SnowFlake snowFlake = new SnowFlake(2,3);//订单号生成器

    @Resource
    DefaultPFNotify defaultPFNotify;
    @Value("${project.gateway-url:}")
    private String gatewayUrl;
    @Value("${project.audit-expire:7}")//7天内未审核自动清除
    private Integer auditExpire;
    @Resource
    private RedisTemplate redisTemplate;
    /**
     * 创建待支付订单
     * @param pay
     * @param payAlonePojo
     */
    public void createWaitPayOrder(Pay pay,PayAlonePojo payAlonePojo){

        PayOrder order = PayOrder.builder()
                .orderId(String.valueOf(snowFlake.nextId()))
                .orderStatus(PayOrder.OrderStatus.WAIT_PAY.value())
                .behooveAmount(pay.getBehoovePrice())
                .realAmount(payAlonePojo.getRealAmount())
                .sellerId(1)
                .userId(1)
                .payExpireTime(payAlonePojo.getExpireDate())
                .payType(pay.getType().value())
                .build();
        mapper.insertSelective(order);

    }

    /**
     * 更新订单状态为待审核
     * @param id
     */
    public void updateStatusToWaitAudit(Long id){
        PayOrder payOrder = mapper.selectByPrimaryKey(id);
        if(null == payOrder){
            return;
        }
        if(payOrder.getOrderStatus() != PayOrder.OrderStatus.WAIT_PAY.value()){
            return;
        }
        payOrder.setOrderStatus(PayOrder.OrderStatus.WAIT_AUDIT.value());
        mapper.updateByPrimaryKey(payOrder);
        String dingHook = "https://oapi.dingtalk.com/robot/send?access_token=98b4514612e60e1fe169c41025fdff82d813e29f9369dc88c955325cf20efdaf";

        //发送审核通知消息
        String uuid = MD5Utils.md5Random();
        String auditUrl = gatewayUrl+"/admin/audit/"+uuid;
        redisTemplate.opsForValue().set(AuditRest.PAY_AUDIT_KEYS+uuid,payOrder.getId(),auditExpire,TimeUnit.DAYS);
        //系统通知审核
        defaultPFNotify.asyncNotifyPayAudit(PayAuditPojo.builder()
                .toHook(dingHook)
                .auditUrl(auditUrl)
                .realAmount(payOrder.getRealAmount().toPlainString())
                .orderId(String.valueOf(payOrder.getId()))
                .orderCreateTime(DateFormat.format(new Date()))
                .build());
    }
}
